/**
 * created by 小卷毛, 2021-08-02
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.MasterData
import com.rzico.sbl.model.PickCategory
import com.rzico.sbl.model.RegionBean
import com.rzico.sbl.model.ResponseModel
import com.xinnuo.common.extend.getJsonFromAssets
import com.xinnuo.common.manager.ContextManager
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

/**
 * 描 述：我的店铺操作相关
 * 作 者：小卷毛  2021-08-02 11:43
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ShopViewModel : CommonViewModel() {

    /**
     * 店铺分类列表
     */
    fun categoryList(
        type: String,
        onSuccess: (ArrayList<PickCategory>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<PickCategory>>>(BaseUrl.mngPickCate)
            .params(
                getParams(
                    "type" to type,
                    "show" to "1"
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<PickCategory>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 店铺列表
     */
    fun shopList() =
        OkGo.get<BaseResponse<ResponseModel<MasterData>>>(BaseUrl.mngShop)
            .params(
                getParams(
                    "pageStart" to 0,
                    "pageSize" to 500
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data.data }

    /**
     * 获取省市区信息，协程流式调用
     *
     * flow上游默认线程为flow方法所处的线程
     */
    fun getAreaList(onNext: (ArrayList<RegionBean>) -> Unit) {
        viewModelScope.launch {
            flow {
                val json =
                    ContextManager
                        .instance
                        .getAppContext()
                        ?.getJsonFromAssets("province.json")
                        ?: "[]"

                emit(
                    Gson().fromJson(
                        json,
                        object : TypeToken<List<RegionBean>>() {}.type
                    ) as ArrayList<RegionBean>
                )
            }
                .flowOn(Dispatchers.Default) //切换上游线程
                .onStart { showLoading() }
                .onCompletion { dismissLoading() }
                .collect {
                    onNext.invoke(it)
                }
        }
    }

}